# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        Young  
# Name:         demo7.09
# Description:
# Author:       $赵怡蕾
# Date:         2019/2/21
#-------------------------------------------------------------------------------

# 定义addNumbers函数，该函数指定了一个可变参数numbers
def addNumbers(*numbers):
    result = 0
    # 枚举numbers中的所有参数值，并将这些值累加，存储到result变量中
    for number in numbers:
        result  += number
    # 返回累加和
    return result

# 调用addNumbers函数，运行结果：15
print(addNumbers(1,2,3,4,5))
print("--------------------")
# 定义calculator函数，在可变参数numbers前面加了一个普通的形象type
def calculator(type,*numbers):
    result = 0
    # type参数的值可为add、sub、mul和div，分别表示加、减、乘和除

    # 进行加法计算
    if type == "add":
        for number in numbers:
            result += number
    # 进行减法计算
    elif type == "sub":
        result = numbers[0]
        for i in range(1,len(numbers)):
            result -= numbers[i]
    # 进行乘法计算
    elif type == "mul":
        result = 0
        for number in numbers:
            result *= number
    # 进行除法计算
    else:
        result = numbers[0]
        for i in range(1,len(numbers)):
            result /= numbers[i]
    # 返回计算结果
    return result

# 调用calculator函数，会将第1个实参作为type参数的值传给calculator函数
# 运行结果：21
print(calculator("add",1,2,3,4,5,6))
# 运行结果：1067
print(calculator("sub",1234,44,54,12,57))
# 运行结果：5040
print(calculator("mul",1,2,3,4,5,6,7))
# 运行结果：10.0
print(calculator("div",100,2,5))
print("------------------")

# 定义calculator1函数，在可变参数numbers后面指定一个普通的形参ratio
def calculator1(type,*numbers,ratio):
    # 在调用另一个函数时，如果该函数的可变参数传入另一个可变参数值，也要在参数名前加星号（*）
    return calculator(type,*numbers) * ratio
# 调用calculator1函数，使用关键字参数指定了ratio参数的值
# 运行结果：63
print(calculator1("add",1,2,3,4,5,6,ratio = 3))
# 运行结果：2134
print(calculator1("sub",1234,44,54,12,57,ratio = 2))
# 运行结果：20160
print(calculator1("mul",1,2,3,4,5,6,7,ratio = 4))
# 运行结果：40.0
print(calculator1("div",100,2,5,ratio = 4))
print("-------------------")
# 定义calculator2函数，为ratio参数指定了一个默认值
def calculator2(type,*numbers,ratio = 4):
    return  calculator(type,*numbers) * ratio
# 调用calculator2函数，没有为ratio参数指定新的值，运行结果：84
print(calculator2("add",1,2,3,4,5,6))
